﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nvigorate.Common;
using Nvigorate.Data;
using Nvigorate.Data.Query;
using Nvigorate.Extensions;
using Nvigorate.Relational.Mapping.Index;
using Nvigorate.Relational.Repository;
using Nvigorate.Relational.State;
using Nvigorate.Relational.Visitors;
using Nvigorate.TestObjects;

namespace Nvigorate.Test.Relational.Repository
{
    /// <summary>
    /// Summary description for PersistVisitorTests
    /// </summary>
    [TestClass]
    public class PersistVisitorTests
    {
        #region Hide

        public PersistVisitorTests()
        {
            //
            // TODO: Add constructor logic here
            //
            MapFake.PopulateMaps();
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #endregion
        
        [TestMethod]
        public void InsertTest1()
        {
            var newPerson = new Person()
            {
                FirstName = "Bilbo",
                LastName = "Jimbo",
                SocialSecurity = "999-99-9990",
                DateOfBirth = DateTime.Parse("01/01/1955"),
                Vehicles = new List<Vehicle>(){new Vehicle(){ Model="Mustang", VIN="197412498", Year=2005 }}
            };

            var query = GetCommandText(newPerson);

            Assert.IsNotNull(query);
        }


        private Tuple<string, QueryList> GetCommandText(ITrackState instance)
        {
            var mapIndex = new MapIndex();
            var repository = "simple";
            var provider = new MSSQL9Provider();
            var repositoryMaps = new RepositoryMapIndex(mapIndex, repository);
            var parameterizer = new Parameterizer();
            var translator = new MSSQL9Visitor();

            var persistVisitor = new PersistVisitor(repositoryMaps, false);
            var persistable = PersistenceDecorator.Create(instance).GetNavigator(repositoryMaps);
            persistable.Accept(persistVisitor);

            var query = persistVisitor.GetQuery();

            var parameters = parameterizer.Parameterize(query, provider, mapIndex.GetRepositoryMapIndex(repository));
            return Tuple.Create(translator.Translate(query), query);
        }
    }
}
